﻿#region Copyright (c) 2013-04, Olaf Kober <amarok.projects@gmail.com>
//================================================================================
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//	copies of the Software, and to permit persons to whom the Software is
//	furnished to do so, subject to the following conditions:
//
//	The above copyright notice and this permission notice shall be included in
//	all copies or substantial portions of the Software.
//
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//	THE SOFTWARE.
//================================================================================
#endregion

#if TESTS

using System;
using System.Threading;
using System.Threading.Tasks;
using NUnit.Framework;


namespace Amarok.Agents
{
	[TestFixture]
	public class Test_MessageHandlerInfo
	{
		private class MyMessage : Message
		{
		}
		private class MyInformation : Information
		{
		}
		private class MyOperation : Operation<String, Int32>
		{
		}

		private class MyClass
		{
			[ConcurrentExecution, MaximumParallelismAttribute]
			public void Method1(String argument)
			{
			}

			public void Method2(Int32 argument)
			{
			}


			public static MyMessage Argument;
			public static Int32 Called;

			[ExclusiveExecution, NoParallelismAttribute]
			public void Method3(MyMessage argument)
			{
				Argument = argument;
				Called++;
			}

			[ExclusiveExecution, NoParallelismAttribute]
			public static void Method4(MyMessage argument)
			{
				Argument = argument;
				Called++;
			}

			[ExclusiveExecution, ModerateParallelismAttribute]
			public static void Method5(MyInformation argument)
			{
			}

			[ConcurrentExecution, NoParallelismAttribute]
			public static void Method6(MyOperation argument)
			{
			}

			[ExclusiveExecution, NoParallelismAttribute]
			public async Task Method7(MyMessage argument)
			{
				await Task.Factory.StartNew(() => Thread.Sleep(100));

				Argument = argument;
				Called++;
			}
		}


		// --- TESTS ---


		[Test]
		public void Test_Construction()
		{
			var target = new MyClass();
			var methodInfo = target.GetType().GetMethod("Method1");
			var parameterInfo = methodInfo.GetParameters()[0];

			var handlerInfo = new MessageHandlerInfo(methodInfo, parameterInfo, false);

			Assert.That(handlerInfo.MethodInfo, Is.EqualTo(methodInfo));
			Assert.That(handlerInfo.ParameterInfo, Is.EqualTo(parameterInfo));
			Assert.That(handlerInfo.IsAsync, Is.False);
			Assert.That(handlerInfo.MessageType, Is.EqualTo(typeof(String)));
			Assert.That(handlerInfo.ExecutionMode, Is.EqualTo(ExecutionMode.Concurrent));
			Assert.That(handlerInfo.ParallelismLevel, Is.EqualTo(ParallelismLevel.MaximumParallelism));
		}

		[Test]
		public void Test_Construction_Defaults()
		{
			var target = new MyClass();
			var methodInfo = target.GetType().GetMethod("Method2");
			var parameterInfo = methodInfo.GetParameters()[0];

			var handlerInfo = new MessageHandlerInfo(methodInfo, parameterInfo, false);

			Assert.That(handlerInfo.MethodInfo, Is.EqualTo(methodInfo));
			Assert.That(handlerInfo.ParameterInfo, Is.EqualTo(parameterInfo));
			Assert.That(handlerInfo.IsAsync, Is.False);
			Assert.That(handlerInfo.MessageType, Is.EqualTo(typeof(Int32)));
			Assert.That(handlerInfo.ExecutionMode, Is.EqualTo(ExecutionMode.Exclusive));
			Assert.That(handlerInfo.ParallelismLevel, Is.EqualTo(ParallelismLevel.NoParallelism));
		}

		[Test]
		public void Test_Construction_InformationAsMessage()
		{
			var target = new MyClass();
			var methodInfo = target.GetType().GetMethod("Method5");
			var parameterInfo = methodInfo.GetParameters()[0];

			var handlerInfo = new MessageHandlerInfo(methodInfo, parameterInfo, false);

			Assert.That(handlerInfo.MethodInfo, Is.EqualTo(methodInfo));
			Assert.That(handlerInfo.ParameterInfo, Is.EqualTo(parameterInfo));
			Assert.That(handlerInfo.IsAsync, Is.False);
			Assert.That(handlerInfo.MessageType, Is.EqualTo(typeof(MyInformation)));
			Assert.That(handlerInfo.ExecutionMode, Is.EqualTo(ExecutionMode.Exclusive));
			Assert.That(handlerInfo.ParallelismLevel, Is.EqualTo(ParallelismLevel.ModerateParallelism));
		}

		[Test]
		public void Test_Construction_OperationAsMessage()
		{
			var target = new MyClass();
			var methodInfo = target.GetType().GetMethod("Method6");
			var parameterInfo = methodInfo.GetParameters()[0];

			var handlerInfo = new MessageHandlerInfo(methodInfo, parameterInfo, false);

			Assert.That(handlerInfo.MethodInfo, Is.EqualTo(methodInfo));
			Assert.That(handlerInfo.ParameterInfo, Is.EqualTo(parameterInfo));
			Assert.That(handlerInfo.IsAsync, Is.False);
			Assert.That(handlerInfo.MessageType, Is.EqualTo(typeof(MyOperation)));
			Assert.That(handlerInfo.ExecutionMode, Is.EqualTo(ExecutionMode.Concurrent));
			Assert.That(handlerInfo.ParallelismLevel, Is.EqualTo(ParallelismLevel.NoParallelism));
		}

		[Test]
		public void Test_CreateHandler_InvokeHandler()
		{
			var target = new MyClass();
			var methodInfo = target.GetType().GetMethod("Method3");
			var parameterInfo = methodInfo.GetParameters()[0];

			var handlerInfo = new MessageHandlerInfo(methodInfo, parameterInfo, false);

			Assert.That(handlerInfo.MethodInfo, Is.EqualTo(methodInfo));
			Assert.That(handlerInfo.ParameterInfo, Is.EqualTo(parameterInfo));
			Assert.That(handlerInfo.IsAsync, Is.False);
			Assert.That(handlerInfo.MessageType, Is.EqualTo(typeof(MyMessage)));
			Assert.That(handlerInfo.ExecutionMode, Is.EqualTo(ExecutionMode.Exclusive));
			Assert.That(handlerInfo.ParallelismLevel, Is.EqualTo(ParallelismLevel.NoParallelism));

			MyClass.Argument = null;
			MyClass.Called = 0;

			var handler = handlerInfo.CreateHandler(target);

			Assert.That(MyClass.Called, Is.EqualTo(0));
			Assert.That(MyClass.Argument, Is.Null);

			var message1 = new MyMessage();
			handler.Invoke(message1);

			Assert.That(MyClass.Called, Is.EqualTo(1));
			Assert.That(MyClass.Argument, Is.SameAs(message1));

			var message2 = new MyMessage();
			handler.Invoke(message2);

			Assert.That(MyClass.Called, Is.EqualTo(2));
			Assert.That(MyClass.Argument, Is.SameAs(message2));
		}

		[Test]
		public void Test_CreateHandler_InvokeHandler_StaticMethod()
		{
			var target = new MyClass();
			var methodInfo = target.GetType().GetMethod("Method4");
			var parameterInfo = methodInfo.GetParameters()[0];

			var handlerInfo = new MessageHandlerInfo(methodInfo, parameterInfo, false);

			Assert.That(handlerInfo.MethodInfo, Is.EqualTo(methodInfo));
			Assert.That(handlerInfo.ParameterInfo, Is.EqualTo(parameterInfo));
			Assert.That(handlerInfo.IsAsync, Is.False);
			Assert.That(handlerInfo.MessageType, Is.EqualTo(typeof(MyMessage)));
			Assert.That(handlerInfo.ExecutionMode, Is.EqualTo(ExecutionMode.Exclusive));
			Assert.That(handlerInfo.ParallelismLevel, Is.EqualTo(ParallelismLevel.NoParallelism));

			MyClass.Argument = null;
			MyClass.Called = 0;

			var handler = handlerInfo.CreateHandler(null);

			Assert.That(MyClass.Called, Is.EqualTo(0));
			Assert.That(MyClass.Argument, Is.Null);

			var message = new MyMessage();
			handler.Invoke(message);

			Assert.That(MyClass.Called, Is.EqualTo(1));
			Assert.That(MyClass.Argument, Is.SameAs(message));
		}

		[Test]
		public void Test_CreateHandler_InvokeHandler_AsyncMethod()
		{
			var target = new MyClass();
			var methodInfo = target.GetType().GetMethod("Method7");
			var parameterInfo = methodInfo.GetParameters()[0];

			var handlerInfo = new MessageHandlerInfo(methodInfo, parameterInfo, true);

			Assert.That(handlerInfo.MethodInfo, Is.EqualTo(methodInfo));
			Assert.That(handlerInfo.ParameterInfo, Is.EqualTo(parameterInfo));
			Assert.That(handlerInfo.IsAsync, Is.True);
			Assert.That(handlerInfo.MessageType, Is.EqualTo(typeof(MyMessage)));
			Assert.That(handlerInfo.ExecutionMode, Is.EqualTo(ExecutionMode.Exclusive));
			Assert.That(handlerInfo.ParallelismLevel, Is.EqualTo(ParallelismLevel.NoParallelism));

			MyClass.Argument = null;
			MyClass.Called = 0;

			var messageHandlerCompletedSignal = new AutoResetEvent(false);
			DefaultDispatcherHooks.MessageHandlerCompleted = (msg) =>
				{
					messageHandlerCompletedSignal.Set();
				};

			var handler = handlerInfo.CreateHandler(target);

			Assert.That(MyClass.Called, Is.EqualTo(0));
			Assert.That(MyClass.Argument, Is.Null);

			var message1 = new MyMessage();
			handler.Invoke(message1);

			messageHandlerCompletedSignal.WaitOne(5000);

			Assert.That(MyClass.Called, Is.EqualTo(1));
			Assert.That(MyClass.Argument, Is.SameAs(message1));

			var message2 = new MyMessage();
			handler.Invoke(message2);

			messageHandlerCompletedSignal.WaitOne(5000);

			Assert.That(MyClass.Called, Is.EqualTo(2));
			Assert.That(MyClass.Argument, Is.SameAs(message2));
		}

		[Test]
		public void Test_Construction_NullMethodInfo()
		{
			var target = new MyClass();
			var methodInfo = target.GetType().GetMethod("Method1");
			var parameterInfo = methodInfo.GetParameters()[0];

			Assert.Throws<ArgumentNullException>(() =>
				{
					var handlerInfo = new MessageHandlerInfo(null, parameterInfo, false);
				});
		}

		[Test]
		public void Test_Construction_NullParameterInfo()
		{
			var target = new MyClass();
			var methodInfo = target.GetType().GetMethod("Method1");

			Assert.Throws<ArgumentNullException>(() =>
				{
					var handlerInfo = new MessageHandlerInfo(methodInfo, null, false);
				});
		}

	}
}

#endif
